
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for post_blog_comment.
	''' </summary>
	Public Partial Class post_blog_comment
		Inherits BaseWebPage
		Protected hlGroupBulletin As System.Web.UI.WebControls.HyperLink
		Protected hlGroupTrash As System.Web.UI.WebControls.HyperLink
		Protected hlGroupBlog As System.Web.UI.WebControls.HyperLink
		'protected System.Web.UI.WebControls.HyperLink hlReturnToBlogList;
		Protected sLinkTag As String = Constants.BLOG_ADD_LINK_TAG
		Protected sImageTag As String = Constants.BLOG_ADD_IMAGE_TAG
		Protected btnAddLink As System.Web.UI.WebControls.Button
		Private blogID As Integer = -1
		Protected grpID As Integer = -1
		Protected grp As Group = Nothing
		Protected usr As User = Nothing
		Protected hlInbox As System.Web.UI.WebControls.HyperLink
		Protected hlSent As System.Web.UI.WebControls.HyperLink
		Protected hlBulletin As System.Web.UI.WebControls.HyperLink
		Protected litBlog As System.Web.UI.WebControls.Literal
		Protected hlGroupBuletin As System.Web.UI.WebControls.HyperLink
		Protected hlTrash As System.Web.UI.WebControls.HyperLink
		Protected litGroupBlog As System.Web.UI.WebControls.Literal
		Protected btnAddImage As System.Web.UI.WebControls.Button
		Protected isGroupMode As Boolean = False
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.POST_BLOG_COMMENT_PAGE;
			usr = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.POST_BLOG_COMMENT, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			' init tabs
			ucBlogTabs.ActiveTab = 2
			ucBlogTabs.PageName = Constants.POST_BLOG_COMMENT
			grpBlogTabs.ActiveTab = 2
			grpBlogTabs.PageName = Constants.POST_BLOG_COMMENT

			' init blog
			If Request.Params("id") = Nothing AndAlso Request.Params("id") = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			Try
				blogID = Convert.ToInt32(Request.Params("id"))
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End Try

			' init group
			If Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
				Try
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
					isGroupMode = True
				Catch
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End Try

				grp = Groups.GetGroupByID(grpID)
				If grp = Nothing Then
					Throw New Exception(Constants.ERROR_MESSAGE_GROUP_DOESNT_EXIST_ERROR)
				End If

				If Not grp.Members.Contains(usr.ID) Then
					Throw New Exception(Constants.ERROR_MESSAGE_BLOG_ONLY_MEMBERS_ACCESS)
				End If
			End If

			If isGroupMode Then
				' init menu
				ucBlogTabs.Visible = False
				grpBlogTabs.Visible = True

				'hlReturnToBlogList.NavigateUrl = "../" + Constants.BLOG + "?" + Constants.GROUP_PARAM + "=" + grp.ID;
				'hlReturnToBlogList.Text =  this.GetPageConstText(htPageConstants, "CONST_GroupReturn_TEXT");
				hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
				hlBackToGroup.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)

				' init page title
				litMainPageTitle.Text = Me.GetPageConstText(htPageConstants, "CONST_GroupTitle_TEXT")
			Else
				ucBlogTabs.Visible = True
					'hlReturnToBlogList.NavigateUrl = "../" + Constants.BLOG;
				grpBlogTabs.Visible = False
			End If
			txtMessage.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_BLOG_MAX_CHARS.ToString()))
			txtMessage.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_BLOG_MAX_CHARS.ToString()))
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.cvHtmlInBody.ServerValidate, AddressOf Me.cvHtmlInBody_ServerValidate

		End Sub
#End Region

		Protected Sub btnPostBlogComment_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If Page.IsValid Then
				'string subject = "";
				Dim body As String = ""

				'subject = txtSubject.Text.Trim();
				body = Server.HtmlDecode(txtMessage.Text.Trim())

				'subject = subject.Substring(0, Math.Min(subject.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS));
				body = body.Substring(0, Math.Min(body.Length, Constants.TEXT_AREA_BLOG_MAX_CHARS))

				If isGroupMode Then
					Dim approve As Boolean = grp.AutoApproveBlogComments
					If LoggedUser.ID = grp.Uid Then
						approve = True
					End If
					Dim grpBlogNew As New GroupBlog(grp.ID, blogID, LoggedUser.ID, "", body, approve)
					grpBlogNew.Send()
					Response.Redirect(Constants.VIEW_BLOG_COMMENTS + "?id=" + blogID + "&" + Constants.GROUP_PARAM + "=" + grp.ID)
				Else
					Dim blogNew As New Blog(blogID, LoggedUser.ID, "", body, True)
					blogNew.Send()
					Response.Redirect(Constants.VIEW_BLOG_COMMENTS + "?id=" + blogID)
				End If
			End If
		End Sub

		Private Sub cvHtmlInBody_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			'	 string fn = Request.Form[txtMessage.UniqueID];
'
'				 if (isHTML(fn))
'						args.IsValid = false;
'				 else
'						args.IsValid = true;
'			

			args.IsValid = True
		End Sub


	End Class
End Namespace






